home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / JRadioButtonMenuItem.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  8.3 KB  |  278 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)JRadioButtonMenuItem.java    1.31 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.util.EventListener;
  17.  
  18. import java.awt.*;
  19. import java.awt.event.*;
  20. import java.awt.image.*;
  21.  
  22. import java.io.ObjectOutputStream;
  23. import java.io.ObjectInputStream;
  24. import java.io.IOException;
  25.  
  26. import javax.swing.plaf.*;
  27. import javax.accessibility.*;
  28.  
  29. /**
  30.  * An implementation of a RadioButtonMenuItem. A RadioButtonMenuItem is
  31.  * a menu item that is part of a group of menu items in which only one
  32.  * item in the group can be selected. The selected item displays its
  33.  * selected state. Selecting it causes any other selected item to
  34.  * switch to the unselected state.
  35.  * <p>
  36.  * Used with a {@link ButtonGroup} object to create a group of menu items
  37.  * in which only one item at a time can be selected. (Create a ButtonGroup
  38.  * object and use its <code>add</code> method to include the JRadioButtonMenuItem
  39.  * objects in the group.)
  40.  * <p>
  41.  * For the keyboard keys used by this component in the standard Look and
  42.  * Feel (L&F) renditions, see the
  43.  * <a href="doc-files/Key-Index.html#JRadioButtonMenuItem">JRadioButtonMenuItem</a> key assignments.
  44.  * <p>
  45.  * <strong>Warning:</strong>
  46.  * Serialized objects of this class will not be compatible with
  47.  * future Swing releases.  The current serialization support is appropriate
  48.  * for short term storage or RMI between applications running the same
  49.  * version of Swing.  A future release of Swing will provide support for
  50.  * long term persistence.
  51.  *
  52.  * @beaninfo
  53.  *   attribute: isContainer false
  54.  *
  55.  * @version 1.31 08/28/98
  56.  * @author Georges Saab
  57.  * @author David Karlton
  58.  * @see ButtonGroup
  59.  */
  60. public class JRadioButtonMenuItem extends JMenuItem implements Accessible {
  61.     /**
  62.      * @see #getUIClassID
  63.      * @see #readObject
  64.      */
  65.     private static final String uiClassID = "RadioButtonMenuItemUI";
  66.  
  67.     /**
  68.      * Creates a JRadioButtonMenuItem with no set text or icon.
  69.      */
  70.     public JRadioButtonMenuItem() {
  71.         this(null, null, false);
  72.     }
  73.  
  74.     /**
  75.      * Creates a JRadioButtonMenuItem with an icon.
  76.      *
  77.      * @param icon the Icon to display on the RadioButtonMenuItem.
  78.      */
  79.     public JRadioButtonMenuItem(Icon icon) {
  80.         this(null, icon, false);
  81.     }
  82.  
  83.     /**
  84.      * Creates a JRadioButtonMenuItem with text.
  85.      *
  86.      * @param text the text of the RadioButtonMenuItem.
  87.      */
  88.     public JRadioButtonMenuItem(String text) {
  89.         this(text, null, false);
  90.     }
  91.     
  92.     /**
  93.      * Creates a JRadioButtonMenuItem with the specified text
  94.      * and Icon.
  95.      *
  96.      * @param text the text of the RadioButtonMenuItem
  97.      * @param icon the icon to display on the RadioButtonMenuItem
  98.      */
  99.     public JRadioButtonMenuItem(String text, Icon icon) {
  100.     this(text, icon, false);
  101.     }
  102.  
  103.     /**
  104.      * Creates a radiobutton menu item with the specified text 
  105.      * and selection state.
  106.      *
  107.      * @param text the text of the CheckBoxMenuItem.
  108.      * @param b the selected state of the checkboxmenuitem
  109.      */
  110.     public JRadioButtonMenuItem(String text, boolean b) {
  111.         this(text);
  112.         setSelected(b);
  113.     }
  114.  
  115.     /**
  116.      * Creates a radio button menu item with the specified image
  117.      * and selection state, but no text.
  118.      *   
  119.      * @param icon  the image that the button should display
  120.      * @param selected  if true, the button is initially selected;
  121.      *                  otherwise, the button is initially unselected
  122.      */
  123.     public JRadioButtonMenuItem(Icon icon, boolean selected) {
  124.         this(null, icon, selected);
  125.     }
  126.  
  127.     /**
  128.      * Creates a radio button menu item that has the specified 
  129.      * text, image, and selection state.
  130.      *
  131.      * @param text  the string displayed on the radio button 
  132.      * @param icon  the image that the button should display
  133.      */
  134.     public JRadioButtonMenuItem(String text, Icon icon, boolean selected) {
  135.         setModel(new JToggleButton.ToggleButtonModel());
  136.         init(text, icon);
  137.         setBorderPainted(false);
  138.         setFocusPainted(false);
  139.         setHorizontalTextPosition(JButton.RIGHT);
  140.         setHorizontalAlignment(JButton.LEFT);
  141.         setSelected(selected);
  142.         updateUI();
  143.     }
  144.     /**
  145.      * Initialize the JRadioButtonMenuItem with the specified text
  146.      * and Icon.
  147.      *
  148.      * @param text the text to display
  149.      * @param icon the icon to display
  150.      */
  151.     protected void init(String text, Icon icon) {
  152.         if(text != null) {
  153.             setText(text);
  154.             
  155.             if(icon != null) {
  156.                 setVerticalTextPosition(BOTTOM);
  157.             } 
  158.         }
  159.         
  160.         if(icon != null) {
  161.             setIcon(icon);
  162.         }
  163.         
  164.         // Listen for Focus events
  165.         addFocusListener(
  166.             new FocusListener() {
  167.             public void focusGained(FocusEvent event) {}
  168.             public void focusLost(FocusEvent event) {
  169.                 // When focus is lost, repaint if 
  170.                 // we focus information is painted
  171.                 if(isFocusPainted()) {
  172.                     repaint();
  173.                 }
  174.             }
  175.         }
  176.         );
  177.     }
  178.     
  179.     
  180.     /**
  181.      * Notification from the UIFactory that the L&F has changed. 
  182.      * Called to replace the UI with the latest version from the 
  183.      * UIFactory.
  184.      *
  185.      * @see JComponent#updateUI
  186.      */
  187.     public void updateUI() {
  188.         setUI((MenuItemUI)UIManager.getUI(this));
  189.     }
  190.  
  191.  
  192.     /**
  193.      * Returns the name of the L&F class that renders this component.
  194.      *
  195.      * @return "RadioButtonMenuItemUI"
  196.      * @see JComponent#getUIClassID
  197.      * @see UIDefaults#getUI
  198.      */
  199.     public String getUIClassID() {
  200.         return uiClassID;
  201.     }
  202.  
  203.  
  204.     /**
  205.      * Override Component.requestFocus() to not grab focus.
  206.      */
  207.     public void requestFocus() {}
  208.  
  209.  
  210.     /** 
  211.      * See readObject() and writeObject() in JComponent for more 
  212.      * information about serialization in Swing.
  213.      */
  214.     private void writeObject(ObjectOutputStream s) throws IOException {
  215.         s.defaultWriteObject();
  216.     if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  217.         ui.installUI(this);
  218.     }
  219.     }
  220.  
  221.  
  222.     /**
  223.      * Returns a string representation of this JRadioButtonMenuItem.
  224.      * This method 
  225.      * is intended to be used only for debugging purposes, and the 
  226.      * content and format of the returned string may vary between      
  227.      * implementations. The returned string may be empty but may not 
  228.      * be <code>null</code>.
  229.      * <P>
  230.      * Overriding paramString() to provide information about the
  231.      * specific new aspects of the JFC components.
  232.      * 
  233.      * @return  a string representation of this JRadioButtonMenuItem.
  234.      */
  235.     protected String paramString() {
  236.     return super.paramString();
  237.     }
  238.  
  239. /////////////////                                                 
  240. // Accessibility support
  241. ////////////////
  242.  
  243.     /**
  244.      * Get the AccessibleContext associated with this JComponent
  245.      *
  246.      * @return the AccessibleContext of this JComponent
  247.      */
  248.     public AccessibleContext getAccessibleContext() {
  249.         if (accessibleContext == null) {
  250.             accessibleContext = new AccessibleJRadioButtonMenuItem();
  251.         }
  252.         return accessibleContext;
  253.     }
  254.  
  255.     /**
  256.      * The class used to obtain the accessible role for this object.
  257.      * <p>
  258.      * <strong>Warning:</strong>
  259.      * Serialized objects of this class will not be compatible with
  260.      * future Swing releases.  The current serialization support is appropriate
  261.      * for short term storage or RMI between applications running the same
  262.      * version of Swing.  A future release of Swing will provide support for
  263.      * long term persistence.
  264.      */
  265.     protected class AccessibleJRadioButtonMenuItem extends AccessibleJMenuItem {
  266.         /**
  267.          * Get the role of this object.
  268.          *
  269.          * @return an instance of AccessibleRole describing the role of the 
  270.          * object
  271.          */
  272.         public AccessibleRole getAccessibleRole() {
  273.             return AccessibleRole.RADIO_BUTTON;
  274.         }
  275.     } // inner class AccessibleJRadioButtonMenuItem
  276. }
  277.  
  278.